<template>
	<view class="">
		<view>
			<view class="content" @tap="textupload">
				发表
			</view>
			<view class="" v-for="item in tempFiles" >
				<image :src="'https://zyc618.mynatapp.cc/'+item.url " mode=""></image>
			</view>
		</view>
		<view class="">
			<tarbar currentPage="list2"></tarbar>
		</view>
	</view>
</template>

<script>
	import tarbar from '@/common/tabbar.vue'
	import $http from '@/common/api/request.js'
	import {mapMutations,mapState} from 'vuex'
	export default {
		data() {
			return {
				tempFiles: [],
				baseurl:" https://zyc618.mynatapp.cc"
			}
		},
		onLoad() {

		},
		computed:{
			...mapState({
				userInfo:state=>state.user.userInfo
			})
		},
		components:{
			tarbar
		},
		methods: {
			textupload(){
				uni.chooseImage({
					count: 6, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album','camera'], //从相册选择
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						for(var i =0;i<tempFilePaths.length;i++){
							const uploadTask = uni.uploadFile({
								url: 'http://192.168.56.1:3000/api/files/upload', //仅为示例，非真实的接口地址
								filePath: tempFilePaths[0],
								name: 'file',
								formData: {
									url: 'text',
									pathname:this.userInfo.id + Date.now()
								},
								success: (uploadFileRes) => {
									// console.log(uploadFileRes);
									let imgdatalist =JSON.parse(uploadFileRes.data);
									if(imgdatalist.code == 200){
										this.tempFiles.push(imgdatalist.data);
									}
									
									
								}
							});
											
							uploadTask.onProgressUpdate((res) => {
								console.log('上传进度' + res.progress);
								console.log('已经上传的数据长度' + res.totalBytesSent);
								console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
							});
						}
						
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.upload {
		position: absolute;
		top: -50rpx;
		right: 20rpx;
		width: 120rpx;
		height: 60rpx;
		line-height: 60rpx;
		background-color: #04c062;
		color: white;
		border-radius: 10rpx;
		text-align: center;
		font-size: 30rpx;
	}

	.content {
		padding: 30rpx;
		margin-top: 70rpx;
		display: flex;
		flex-wrap: wrap;

		.files {
			width: 200rpx;
			height: 200rpx;
			background-color: #f7f7f7;
			color: #6d6d6d;
			font-size: 100rpx;
			font-weight: 100;
			display: flex;
			justify-content: center;
			align-items: center;
			margin: 10rpx;
		}

		.images {
			width: 200rpx;
			height: 200rpx;
			margin: 10rpx;
			position: relative;

			.img {
				width: 100%;
				height: 100%;
			}

			.remove {
				width: 50rpx;
				height: 50rpx;
				background-color: #f7f7f7;
				position: absolute;
				top: -5rpx;
				right: -10rpx;
				text-align: center;
				opacity: .5;
				border-radius: 0 0 0 50rpx;
				color: #c5c5c5;
			}
		}
	}
</style>
